import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from "../views/Home.vue"
import store from "@/store/index.js"
//路由懒加载
// const Home = () => import('../views/Home.vue')



Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home,
    redirect: "/homelaybel",
    children: [
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../views/Goods.vue')
      },
      {
        path: '/about',
        name: 'about',
        component: () => import('../views/About.vue')
      },
      {
        path: '/homelaybel',
        name: 'homelaybel',
        component: () => import('../views/HomeLaybel.vue')
      },
      {
        path: '/goods/details',
        name: 'details',
        component: () => import('../views/Details.vue')
      },
      {
        path: '/order',
        name: 'order',
        component: () => import('../views/Order.vue')
      },
      {
        path: '/collect',
        name: 'collect',
        component: () => import('../views/Collect.vue')
      },
      {
        path: '/shoppingCart',
        name: 'shoppingCart',
        component: () => import('../views/ShoppingCart.vue')
      },
      {
        path: '/confirmOrder',
        name: 'confirmOrder',
        component: () => import('../views/ConfirmOrder.vue')
      },
      {
        path: '/text',
        name: 'text',
        component: () => import('../views/Text.vue')
      },
      {
        path: '/menu',
        name: 'Menu',
        component: () => import('@/views/Menu.vue'),
      },
    ]
  },
]
const router = new VueRouter({
  routes
})
//路由守卫-进行全局路由拦截


const whiteList = ['/test', '/homelaybel', '/goods', '/about', "/menu", "/goods/details"];

router.beforeEach((to, from, next) => {
  let userId = store.state.users.user_id;
  console.log(userId);
  if (userId) {
    next()
  } else {
    if (whiteList.indexOf(to.path) != -1) {
      next()
    }
    else {
      next('/')
    }
  }
})



export default router
